package jmine.tec.services.api.errorlistener;

import bancosys.tec.rtm.ExceptionContext;
import bancosys.tec.rtm.exception.ExceptionPersister;
import jmine.tec.services.api.Service;
import jmine.tec.services.api.ServiceAssertionFailedError;
import jmine.tec.services.api.ServiceExecutionException;

/**
 * @author takeshi
 */
public class RTMErrorListener implements ServiceErrorListener {

    private ExceptionPersister exceptionPersister;

    /**
     * {@inheritDoc}
     * 
     * @see jmine.tec.services.api.errorlistener.ServiceErrorListener#handleServiceError(jmine.tec.services.api.Service,
     * jmine.tec.services.api.ServiceExecutionException)
     */
    public void handleServiceError(Service<?> service, ServiceExecutionException serviceExecutionException) {
        this.handleServiceThrowable(service, serviceExecutionException);
    }

    /**
     * {@inheritDoc}
     * 
     * @see jmine.tec.services.api.errorlistener.ServiceErrorListener#handleServiceFailure(jmine.tec.services.api.Service,
     * jmine.tec.services.api.ServiceAssertionFailedError)
     */
    public void handleServiceFailure(Service<?> service, ServiceAssertionFailedError serviceAssertionFailedError) {
        handleServiceThrowable(service, serviceAssertionFailedError);
    }

    /**
     * {@inheritDoc}
     * 
     * @see jmine.tec.services.api.errorlistener.ServiceErrorListener#handleServiceThrowable(jmine.tec.services.api.Service,
     * java.lang.Throwable)
     */
    public void handleServiceThrowable(Service<?> service, Throwable throwable) {
        String serviceName = service.getServiceName();
        String action = service.getAction().toString();
        String properties = service.getServiceProperties().toString();
        
        ExceptionContext context = ExceptionContext.getCurrentContext();
        context.put("SERVICE NAME", serviceName);
        context.put("SERVICE ACTION", action);
        context.put("SERVICE PROPERTIES", properties);
        this.exceptionPersister.monitor(throwable);

    }

    /**
     * @param exceptionPersister the exceptionPersister to set
     */
    public void setExceptionPersister(ExceptionPersister exceptionPersister) {
        this.exceptionPersister = exceptionPersister;
    }

}
